package com.project.decoration.controller.house;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.decoration.common.enums.BusinessType;
import com.project.decoration.common.enums.ResultCode;
import com.project.decoration.domain.DecorationCase;
import com.project.decoration.domain.DecorationHouseType;
import com.project.decoration.domain.DecorationPrice;
import com.project.decoration.domain.Result;
import com.project.decoration.service.DecorationCaseService;
import com.project.decoration.service.DecorationHouseTypeService;
import com.project.decoration.service.DecorationPriceService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author 超级管理员
 * @version 1.0
 * @description: 户型controller
 * @date 2025/02/13 08:52
 */
@Controller
@ResponseBody
@RequestMapping("type")
public class DecorationHouseTypeController {

    @Autowired
    private DecorationHouseTypeService decorationHouseTypeService;
    @Autowired
    private DecorationPriceService decorationPriceService;
    @Autowired
    private DecorationCaseService decorationCaseService;

    /** 分页获取户型 */
    @PostMapping("getDecorationHouseTypePage")
    public Result getDecorationHouseTypePage(@RequestBody DecorationHouseType decorationHouseType) {
        Page<DecorationHouseType> page = new Page<>(decorationHouseType.getPageNumber(),decorationHouseType.getPageSize());
        QueryWrapper<DecorationHouseType> queryWrapper = new QueryWrapper<>();
        queryWrapper.lambda()
                .like(StringUtils.isNotBlank(decorationHouseType.getName()),DecorationHouseType::getName,decorationHouseType.getName());
        Page<DecorationHouseType> decorationHouseTypePage = decorationHouseTypeService.page(page, queryWrapper);
        return Result.success(decorationHouseTypePage);
    }

    @GetMapping("getDecorationHouseTypeList")
    public Result getDecorationHouseTypeList() {
        List<DecorationHouseType> typeList = decorationHouseTypeService.list();
        return Result.success(typeList);
    }

    /** 根据id获取户型 */
    @GetMapping("getDecorationHouseTypeById")
    public Result getDecorationHouseTypeById(@RequestParam("id")String id) {
        DecorationHouseType decorationHouseType = decorationHouseTypeService.getById(id);
        return Result.success(decorationHouseType);
    }

    /** 保存户型 */
    @PostMapping("saveDecorationHouseType")
    public Result saveDecorationHouseType(@RequestBody DecorationHouseType decorationHouseType) {
        boolean save = decorationHouseTypeService.save(decorationHouseType);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 编辑户型 */
    @PostMapping("editDecorationHouseType")
    public Result editDecorationHouseType(@RequestBody DecorationHouseType decorationHouseType) {
        boolean save = decorationHouseTypeService.updateById(decorationHouseType);
        if (save) {
            return Result.success();
        } else {
            return Result.fail(ResultCode.COMMON_DATA_OPTION_ERROR.getMessage());
        }
    }

    /** 删除户型 */
    @GetMapping("removeDecorationHouseType")
    public Result removeDecorationHouseType(@RequestParam("ids")String ids) {
        if (StringUtils.isNotBlank(ids)) {
            String[] asList = ids.split(",");
            for (String id : asList) {
                DecorationHouseType houseType = decorationHouseTypeService.getById(id);
                QueryWrapper<DecorationCase> queryWrapper = new QueryWrapper<>();
                queryWrapper.lambda().eq(DecorationCase::getType,houseType.getName());
                int count = decorationCaseService.count(queryWrapper);
                if (count > 0) {
                    return Result.fail("户型下存在案例，无法删除");
                }
                QueryWrapper<DecorationPrice> wrapper = new QueryWrapper<>();
                wrapper.lambda().like(DecorationPrice::getType,houseType.getName());
                int counted = decorationPriceService.count(wrapper);
                if (counted > 0) {
                    return Result.fail("户型下存在套餐，无法删除");
                }
                decorationHouseTypeService.removeById(id);
            }
            return Result.success();
        } else {
            return Result.fail("户型id不能为空！");
        }
    }

}
